/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zproekt.servis;

import com.zproekt.entity.Pacient;
import com.zproekt.entity.PrivremenTermin;
import com.zproekt.entity.Termin;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Ruzin
 */
public class OrdinacijaCRUD {

    public static List<Pacient> getAllPacienti() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Pacient> result = session.createCriteria(Pacient.class).addOrder(Order.asc("ime")).list();
        session.getTransaction().commit();
        return result;
    }

    public static List<Termin> getAllTermini() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Termin> result = session.createCriteria(Termin.class).addOrder(Order.asc("datum")).list();
        session.getTransaction().commit();
        return result;
    }
    
    public static List<PrivremenTermin> getAllTempTermini() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<PrivremenTermin> result = session.createCriteria(PrivremenTermin.class).addOrder(Order.asc("datum")).list();
        session.getTransaction().commit();
        return result;
    }

    public static Long createPacient(String embg, String ime, String prezime, String credential) {
        Pacient pacient = new Pacient();
        pacient.setEmbg(embg);
        pacient.setIme(ime);
        pacient.setPrezime(prezime);
        pacient.setCredential(credential);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Long rezultat = (Long) session.save(pacient);
        session.getTransaction().commit();
        return rezultat;
    }

    public static Long createTermin(Date datum, Date vreme, Pacient pacient, String tretman) {
        Termin termin = new Termin();
        termin.setDatum(datum);
        termin.setPacient(pacient);
        termin.setTretman(tretman);
        termin.setVreme(vreme);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Long rezultat = (Long) session.save(termin);
        session.getTransaction().commit();
        return rezultat;
    }
    
    public static Long createTempTermin(Date datum, Date vreme, Pacient pacient, String tretman) {
        PrivremenTermin termin = new PrivremenTermin();
        termin.setDatum(datum);
        termin.setPacient(pacient);
        termin.setTretman(tretman);
        termin.setVreme(vreme);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Long rezultat = (Long) session.save(termin);
        session.getTransaction().commit();
        return rezultat;
    }

    public static void updatePacient(Long id, String embg, String ime, String prezime, String credential) {
        Pacient pacient = new Pacient();
        pacient.setId(id);
        pacient.setEmbg(embg);
        pacient.setIme(ime);
        pacient.setPrezime(prezime);
        pacient.setCredential(credential);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(pacient);
        session.getTransaction().commit();
    }

    public static void updateTermin(Long id, Pacient pacient, Date datum, Date vreme, String tretman) {
        Termin termin = new Termin();
        termin.setDatum(datum);
        termin.setId(id);
        termin.setPacient(pacient);
        termin.setTretman(tretman);
        termin.setVreme(vreme);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(termin);
        session.getTransaction().commit();
    }

    public static Pacient getPacient(Long id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Pacient result = (Pacient) session.get(Pacient.class, id);
        session.getTransaction().commit();
        return result;
    }

    public static Termin getTermin(Long id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Termin result = (Termin) session.get(Pacient.class, id);
        session.getTransaction().commit();
        return result;
    }
    
    public static PrivremenTermin getTempTermin(Long id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        PrivremenTermin result = (PrivremenTermin) session.get(PrivremenTermin.class, id);
        session.getTransaction().commit();
        return result;
    }

    public static List<Termin> getTerminUnijaPacient(Long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Pacient pacient = getPacient(id);
        List<Termin> result = session.createCriteria(Termin.class).add(Restrictions.eq("pacient", pacient)).list();
        session.getTransaction().commit();
        return result;
    }

    public static void deletePacient(Pacient pacient) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(pacient);
        session.getTransaction().commit();
    }
    
    public static void deleteTempTermin(PrivremenTermin termin) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(termin);
        session.getTransaction().commit();
    }

   public static List<Termin> getTerminByDate(String data){
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String myDate = data;
            Date datavn = null;
        try {
            datavn = formatter.parse(myDate);
        } catch (ParseException ex) {
            Logger.getLogger(OrdinacijaCRUD.class.getName()).log(Level.SEVERE, null, ex);
        }
            List<Termin> listaSite = session.createCriteria(Termin.class).addOrder(Order.asc("datum")).list();
            List<Termin> result = new ArrayList<Termin>() ;
            for (int i = 0; i < listaSite.size(); i++) {
                if (listaSite.get(i).getDatum().equals(datavn)) result.add(listaSite.get(i));                 
            }
            session.getTransaction().commit();
            return result;   
    }
}
